package com.myhexin.globalbroker.rwa.common.util;

import com.myhexin.globalbroker.rwa.common.component.solidity.MySecurityToken;
import lombok.extern.slf4j.Slf4j;
import org.web3j.codegen.SolidityFunctionWrapperGenerator;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.RemoteFunctionCall;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.Contract;
import org.web3j.tx.gas.DefaultGasProvider;
import org.web3j.tx.gas.StaticGasProvider;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.stream.Stream;

/**
 * @auther zhangzhonglv@myhexin.com
 * @date 2025/9/30
 */
@Slf4j
public final class Web3Util {
    private Web3Util() {
    }

    /**
     * 生成合约的java代码
     * 其中 -p 为生成java代码的包路径此参数和 -o 参数配合使用，以便将java文件放入正确的路径当中
     *
     * @param abiFile      abi的文件路径
     * @param binFile      bin的文件路径
     * @param generateFile 生成的java文件路径
     */
    public static void generateClass(String abiFile, String binFile, String generateFile) {
        String[] args = Arrays.asList(
                "-a", abiFile,
                "-b", binFile,
                "-p", "",
                "-o", generateFile
        ).toArray(new String[0]);
        Stream.of(args).forEach(System.out::println);
        SolidityFunctionWrapperGenerator.main(args);
    }

    public static String deployContract() throws Exception {
        Web3j web3 = Web3j.build(new HttpService("https://sepolia.infura.io/v3/731ec37d4ddc4dc28d4e4d6399ed08c2"));
        Credentials credentials = Credentials.create("d18606f910c0879426f271e3d813d0ba63a84c8a57e1138a2b7b433c8b2f9cb3");
        MySecurityToken contract = MySecurityToken.deploy(
                web3, credentials, web3.ethGasPrice().send().getGasPrice(), Contract.GAS_LIMIT).send();
        return contract.getContractAddress();
    }

    public static void callContract() throws Exception {
        Web3j web3 = Web3j.build(new HttpService("https://sepolia.infura.io/v3/731ec37d4ddc4dc28d4e4d6399ed08c2"));
        Credentials credentials = Credentials.create("d18606f910c0879426f271e3d813d0ba63a84c8a57e1138a2b7b433c8b2f9cb3");
        MySecurityToken contract = MySecurityToken.load("0x7a16efa1c0fcd39a00fdd7a9c51663c4c04a450e", web3, credentials,
                new StaticGasProvider(web3.ethGasPrice().send().getGasPrice(), DefaultGasProvider.GAS_LIMIT));

        RemoteFunctionCall<TransactionReceipt> setValue = contract.set(BigInteger.valueOf(100));
        TransactionReceipt transactionReceipt = setValue.send();
        String transactionHash = transactionReceipt.getTransactionHash();
        log.info("transactionHash:{}", transactionHash);
        String blockHash = transactionReceipt.getBlockHash();
        log.info("blockHash:{}", blockHash);

        RemoteFunctionCall<BigInteger> bigIntegerRemoteFunctionCall = contract.get();
        BigInteger value = bigIntegerRemoteFunctionCall.send();
        log.info("get value:{}", value);
    }

    public static void main(String[] args) throws Exception {
//        String abiFile = "src/main/resources/contract/build/MyTestToken.sol.abi";
//        String binFile = "src/main/resources/contract/build/MyTestToken.sol.bin";
//        String generateFile = "src/main/java/com/myhexin/globalbroker/rwa/common/component/solidity";
//        generateClass(abiFile, binFile, generateFile);

//        String address = deployContract();
//        log.info("合约地址:{}", address);
        // 合约地址:0x7a16efa1c0fcd39a00fdd7a9c51663c4c04a450e

        callContract();
    }
}
